package com.yzt.dynamicmodule.adapter;

import android.graphics.Color;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.viewpager.widget.ViewPager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.module.LoadMoreModule;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.yzt.dynamicmodule.R;
import com.yzt.dynamicmodule.banner.IndexBannerViewHolder;
import com.yzt.dynamicmodule.view.IndexPageIndicatorView;
import com.yzt.plugin_imageloader.ImageLoadUtil;
import com.yzt.zhuju.RXingUtils;
import com.yzt.zhuju.network.entity.home.IndexDataBean;
import com.yzt.zhuju.utils.FormatUtils;
import com.yzt.zhuju.widget.tuiview.TUIImageView;
import com.yzt.zhuju.widget.tuiview.TUITextView;
import com.yzt.zhuju.widget.widget.RCImageView;
import com.zhouwei.mzbanner.MZBannerView;
import com.zhouwei.mzbanner.holder.MZHolderCreator;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * author : bob
 * date   : 2022/11/15
 * desc   : 首页的发现列表
 */
public class IndexDiscoverAdapter extends BaseQuickAdapter<IndexDataBean, BaseViewHolder> implements LoadMoreModule {

    public IndexDiscoverAdapter() {
        super(R.layout.item_index_discover);
    }

    @Override
    protected void convert(@NotNull BaseViewHolder holder, IndexDataBean data) {
        FrameLayout flParentContainer = holder.getView(R.id.fl_parent_container);
        LinearLayout llLeftBottom = holder.getView(R.id.ll_left_bottom);
        MZBannerView bannerView = holder.getView(R.id.banner_view);
        IndexPageIndicatorView indicator = holder.getView(R.id.indicator);
        FrameLayout videoContainer = holder.getView(R.id.video_container);
        TextView tv_contentInfo = holder.getView(R.id.tv_contentInfo);
        FrameLayout textContainer = holder.getView(R.id.text_container);

        llLeftBottom.setVisibility(View.VISIBLE);
        holder.setVisible(R.id.tv_banner_count, false);

        if (data.getMoment() != null) {
            if (data.getMoment().getContentType() == 1) {
                // 轮播
                bannerView.setVisibility(View.VISIBLE);
                videoContainer.setVisibility(View.GONE);
                textContainer.setVisibility(View.GONE);
                initBanner(bannerView, data, holder);
                holder.setText(R.id.tv_title, data.getMoment().getTitle());
                holder.setText(R.id.tv_content, data.getMoment().getContent());
                holder.setVisible(R.id.tv_banner_count, true);
                holder.setText(R.id.tv_banner_count, "1/" + data.getMoment().getMomentUrls().size());
            } else if (data.getMoment().getContentType() == 2) {
                // 视频
                bannerView.setVisibility(View.GONE);
                videoContainer.setVisibility(View.VISIBLE);
                textContainer.setVisibility(View.GONE);
                holder.setText(R.id.tv_title, data.getMoment().getTitle());
                holder.setText(R.id.tv_content, data.getMoment().getContent());
            } else {
                // 文字
                bannerView.setVisibility(View.GONE);
                videoContainer.setVisibility(View.GONE);
                textContainer.setVisibility(View.VISIBLE);
                llLeftBottom.setVisibility(View.GONE);
                tv_contentInfo.setText(data.getMoment().getContent());
                flParentContainer.setBackgroundColor(Color.parseColor(data.getMoment().getBgColor()));
                if (data.getMoment().getBgColor().equals("#000000")) {
                    tv_contentInfo.setTextColor(Color.parseColor("#FFFFFF"));
                } else {
                    tv_contentInfo.setTextColor(Color.parseColor("#000000"));
                }
            }
        } else if (data.getActivity() != null) {

            // 是否需要付费
            if (data.isShow()) {
                if (data.isNeedToPay()) {
                    holder.setVisible(R.id.tv_not_pay, false);
                    holder.setVisible(R.id.tv_need_pay, true);
                } else {
                    holder.setVisible(R.id.tv_not_pay, true);
                    holder.setVisible(R.id.tv_need_pay, false);
                }
            }

            textContainer.setVisibility(View.GONE);

            String path = data.getActivity().getActivityFileUrl();
            if (path.equals("mp4") || path.equals("MP4")) {
                // 视频
                bannerView.setVisibility(View.GONE);
                videoContainer.setVisibility(View.VISIBLE);
            } else {
                // 轮播
                bannerView.setVisibility(View.VISIBLE);
                videoContainer.setVisibility(View.GONE);
                List<String> cover = new ArrayList<>();
                cover.add(path);
                initBanner(bannerView, cover, indicator);
                holder.setVisible(R.id.tv_banner_count, false);
            }

            holder.setText(R.id.tv_title, data.getInfo());
            holder.setText(R.id.tv_content, data.getActivity().getTimeStr());
        }

        // 头像
        RCImageView avatar = holder.getView(R.id.riv_avatar);
        ImageLoadUtil.loadAvatar(getContext(), data.getAvatar(), avatar, R.mipmap.icon_placeholder_long);

        // 关注状态
        TUIImageView imgAdd = holder.getView(R.id.img_add);
        imgAdd.setSelected(data.isFollowed());

        if (String.valueOf(data.getUserId()).equals(RXingUtils.getPersonDetail().getUserBasic().getUserId())) {
            imgAdd.setVisibility(View.INVISIBLE);
        } else {
            imgAdd.setVisibility(View.VISIBLE);
        }

        // 地址
        if (data.getDistance() > 1000) {
            holder.setText(R.id.tv_position, data.getLocation() + " · " + FormatUtils.formatKeepOne(data.getDistance() / 1000) + "km");
        } else if (data.getDistance() != 0) {
            holder.setText(R.id.tv_position, data.getLocation() + " · " + FormatUtils.formatKeepOne(data.getDistance()) + "m");
        } else {
            holder.setText(R.id.tv_position, data.getLocation());
        }

        TUITextView like = holder.findView(R.id.tv_like);
        TUITextView comment = holder.findView(R.id.tv_comment);

        if (data.getMoment() != null) {
            // 点赞数
            holder.setText(R.id.tv_like, String.valueOf(data.getMoment().getLikeNum()));
            like.setSelected(data.getMoment().isLiked());
            // 评论数据
            holder.setText(R.id.tv_comment, String.valueOf(data.getMoment().getCommentNum()));
            like.setVisibility(View.VISIBLE);
            comment.setVisibility(View.VISIBLE);
        } else {
            like.setVisibility(View.INVISIBLE);
            comment.setVisibility(View.INVISIBLE);
        }

        if (data.getShareNum() > 0) {
            holder.setText(R.id.tv_share, String.valueOf(data.getShareNum()));
        } else {
            holder.setText(R.id.tv_share, "转发");
        }
    }

    /**
     * 加载轮播图
     */
    private void initBanner(MZBannerView bannerView, IndexDataBean data, BaseViewHolder holder) {
        if (data.getMoment() != null && data.getMoment().getMomentUrls() != null && !data.getMoment().getMomentUrls().isEmpty()) {
            bannerView.addPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    holder.setText(R.id.tv_banner_count, (position + 1) + "/" + data.getMoment().getMomentUrls().size());
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });
            bannerView.setIndicatorVisible(false);
            bannerView.setPages(data.getMoment().getMomentUrls(), (MZHolderCreator<IndexBannerViewHolder>) IndexBannerViewHolder::new);
            bannerView.setCanLoop(true);
            bannerView.start();
        }
    }

    /**
     * 加载轮播图
     */
    private void initBanner(MZBannerView bannerView, List<String> data, IndexPageIndicatorView indicator) {
        bannerView.addPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                indicator.setPersonalSelectedPage(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        bannerView.setIndicatorVisible(false);
        bannerView.setPages(data, (MZHolderCreator<IndexBannerViewHolder>) IndexBannerViewHolder::new);
        bannerView.setCanLoop(false);
    }
}